package com.banzhi.shop.model.Impl;

import android.util.Log;

import com.banzhi.shop.MyApp;
import com.banzhi.shop.entity.Group.GroupShop;
import com.banzhi.shop.entity.Group.GroupShops;
import com.banzhi.shop.entity.Group.ShopGood;
import com.banzhi.shop.model.CallBack.ManagementCallBack;
import com.banzhi.shop.model.IGroupShop;
import com.banzhi.shop.state.URLString;
import com.banzhi.shop.utils.LoadingDialogUtils;
import com.banzhi.shop.utils.LogUtils;
import com.banzhi.shop.utils.ToastUtils;
import com.google.gson.Gson;

import org.json.JSONException;
import org.json.JSONObject;
import org.xutils.common.Callback;
import org.xutils.x;
import org.xutils.http.RequestParams;

/**
 * Created by Administrator on 2018/3/14 0014.
 */

public class GroupShopImpl implements IGroupShop {

    @Override
    public void getGroup(String brandId,int page, final ManagementCallBack callBack) {
     // RequestParams params = RequestParams(URLString.hostUrl + URLString.groupShop);
        RequestParams params = new RequestParams(URLString.hostUrl + URLString.groupShop);
        params.addBodyParameter("token", MyApp.loginMessage.getString("token", ""));
        params.addBodyParameter("page", page + "");
        params.addBodyParameter("brandid", brandId);
        x.http().post(params, new Callback.CommonCallback<String>() {
            @Override
            public void onSuccess(String s) {
                LogUtils.d("jiejie", s);
                try {
                    JSONObject object = new JSONObject(s);
                    if (object.optString("code").equals("0")) {
                        Gson gson = new Gson();
                        GroupShops groupShop = gson.fromJson(s, GroupShops.class);
                        callBack.ongetDataSuccess(groupShop.getRes());
                    } else {
                        callBack.ongetDataError(object);
                    }
                } catch (JSONException e) {
                    Log.d("jiejie","----" + e);
                    e.printStackTrace();
                }
            }

            @Override
            public void onError(Throwable throwable, boolean b) {
                LoadingDialogUtils.dismiss();
                Log.d("jiejie","----" + throwable);
                ToastUtils.makeToastShort("网络加载失败,请稍后再试(11)");
            }

            @Override
            public void onCancelled(CancelledException e) {

            }

            @Override
            public void onFinished() {

            }
        });
    }

    /**
     * 获取某个品牌的商品列表页面接口
     * http://banzhi.wxmarket.cn/shop/shopgoods/brandgoodslist
     */
    @Override
    public void getGroupDetail(int brandId, int page, final MyCallback callback) {
        org.xutils.http.RequestParams params = new org.xutils.http.RequestParams(URLString.hostUrl + URLString.groupShopList);
        params.addBodyParameter("token", MyApp.loginMessage.getString("token", ""));
        params.addBodyParameter("page", page + "");
        params.addBodyParameter("brandid", brandId + "");
        x.http().post(params, new Callback.CommonCallback<String>() {
            @Override
            public void onSuccess(String s) {
                LogUtils.d("jiejie", s);
                try {
                    JSONObject object = new JSONObject(s);
                    if (object.optString("code").equals("0")) {
                        Gson gson = new Gson();
                        ShopGood groupShop = gson.fromJson(s, ShopGood.class);
                        callback.onSuccess(groupShop.getData());
                    } else {
                        callback.onError(object);
                    }
                } catch (JSONException e) {
                    Log.d("jiejie","----" + e);
                    e.printStackTrace();
                }
            }

            @Override
            public void onError(Throwable throwable, boolean b) {
                LoadingDialogUtils.dismiss();
                Log.d("jiejie","----" + throwable);
                ToastUtils.makeToastShort("网络加载失败,请稍后再试(10)");
            }

            @Override
            public void onCancelled(CancelledException e) {

            }

            @Override
            public void onFinished() {

            }
        });
    }

}
